package com.wyp.design.pattern.creational.singleton.hungry;


import java.io.Serializable;

/**
 * 饿汉式中变量是可以使用final进行修饰的，懒汉式不可以
 *
 * @author: yipeng.wang01@hand-china.com-25490
 * @TIME: 2020/1/2/17:00
 **/
public class HungrySingleton implements Serializable, Cloneable {
    private final static HungrySingleton hungrySingleton;
    //当然初始化操作也可以放在静态代码块中

    static {
        hungrySingleton = new HungrySingleton();
    }

    private HungrySingleton() {
    }

    public static HungrySingleton getInstance() {
        return hungrySingleton;
    }

    /**
     * 如何防止克隆对单例的破坏
     * 1.不实现Cloneable接口，
     * 2.在clone方法中返回getInstance对象
     *
     * @return
     * @throws CloneNotSupportedException
     */
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return getInstance();
    }
}
